package io.gbznt.modules.buss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.gbznt.common.utils.Constant;
import io.gbznt.common.utils.ShiroUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.Query;

import io.gbznt.modules.buss.dao.BussCameraInfoDao;
import io.gbznt.modules.buss.entity.BussCameraInfoEntity;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.service.BussCameraInfoService;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.sys.entity.SysDeptEntity;
import io.gbznt.modules.sys.service.SysDeptService;

@Service("bussCameraInfoService")
public class BussCameraInfoServiceImpl extends ServiceImpl<BussCameraInfoDao, BussCameraInfoEntity>
		implements BussCameraInfoService {
	@Autowired
	private BussFarmlandInfoService bussFarmlandInfoService;
	@Autowired
	private SysDeptService sysDeptService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String name = (String) params.get("name");
		IPage<BussCameraInfoEntity> page = this.page(new Query<BussCameraInfoEntity>().getPage(params),
				new QueryWrapper<BussCameraInfoEntity>().like(StringUtils.isNotBlank(name), "serial_num", name)
						// 个人数据权限控制
						.in(!Constant.SUPER_ADMIN.equals(ShiroUtils.getUserId()),
						"dept_id", sysDeptService.getPermissionDeptIdsArr(true,true))
						.orderByAsc("farmland_info_id").orderByDesc("create_time"));

		for (BussCameraInfoEntity bcie : page.getRecords()) {
			if (StringUtils.isNotBlank(bcie.getFarmlandInfoId())) {
				BussFarmlandInfoEntity bussFarmlandInfo = bussFarmlandInfoService.getById(bcie.getFarmlandInfoId());
				if (bussFarmlandInfo != null && StringUtils.isNotBlank(bussFarmlandInfo.getName())) {
					bcie.setTkName(bussFarmlandInfo.getName());
				}
			}
			if (StringUtils.isNotBlank(bcie.getDeptId())) {
				SysDeptEntity dept = sysDeptService.getById(bcie.getDeptId());
				if (dept != null && StringUtils.isNotBlank(dept.getName())) {
					bcie.setDeptName(dept.getName());
				}
			}

		}
		return new PageUtils(page);
	}

}
